# encoding:utf-8
#qpy:console
import my_importer
my_importer.myImport("myTools")
from myTools import *
import hashlib
huLuXia = {
	    "username" : "",
	    "password" : "",
	    "key" : ""
	}

def init():
    #初始化
    
    global fileName
    global session
    session = requests.session()
    global fileName
    fileName = "huLuXia/huLuXia.json"
    global huLuXia
    huLuXia = getFile(fileName)
    
def login():
    url = 'http://floor.huluxia.com/account/login/ANDROID/4.1.8?platform=2&gkey=000000&app_version=4.1.0.8.2&versioncode=20141461&market_id=floor_xiaomi&_key=&device_code=%5Bd%5D78fbf115-d20f-431b-8325-2aeed82e74af&phone_brand_type=MI'
    data = {
        'account': huLuXia["username"],
        'login_type': '2',
        'password': hashlib.md5(huLuXia["password"].encode()).hexdigest(),
        'sign': '90E8E180FC9BBDC2C09BD0272F56DE8F'
    }
    session.headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        "User-Agent": "okhttp/3.8.1"
        }
    login_res = session.post(url=url,data=data)
    print(login_res.text)
    login_res = login_res.json()
    nick = login_res['user']['nick']
    print('登录用户：',nick)
    huLuXia["key"] = login_res['_key']     
    
def check():
    url1 = 'http://floor.huluxia.com/user/status/ANDROID/2.1'
    params = {
        'platform': '2',
        'gkey': '000000',
        'app_version': '4.0.0.6.3',
        'versioncode': '20141434',
        'market_id': 'floor_baidu',
        '_key': huLuXia["key"],
        'device_code': '%5Bw%5D02%3A00%3A00%3A00%3A00%3A00',
    }
    check_req = session.get(url=url1,params=params)
    check_req = check_req.json()
    status = check_req['status']
    if status == 0:
        print('令牌验证失败')
        return False
    elif status == 1:
        print('令牌验证成功')
        return True


def category():
    global titles
    global categoryIDs
    titles = []
    categoryIDs = []
    url = 'http://floor.huluxia.com/category/list/ANDROID/2.0'
    params = {
        'platform': '2',
        'gkey': '000000',
        'app_version': '4.0.0.6.3',
        'versioncode': '20141434',
        'market_id': 'floor_huluxia',
        '_key': huLuXia["key"],
        'device_code': '%5Bw%5D02%3A00%3A00%3A00%3A00%3A00',
        'is_hidden': '1'
    }
    category_res = session.get(url=url,params=params)
    
    category_res = category_res.json()
    category_res = category_res["categories"]
    for i in range(len(category_res)):
        res = category_res[i]
        if res['categoryID'] != 0:
            titles.append(res['title'])            
            categoryIDs.append(res['categoryID'])   
     
    return titles,categoryIDs

def signin():
    
    url = f'http://floor.huluxia.com/user/signin/ANDROID/4.0'
    msg = ""
    for i in range(len(categoryIDs)):
        IDS = str(categoryIDs[i])
        title = titles[i]
        params = {
            'platform': '2',
            'gkey': '000000',
            'app_version': '4.0.0.6.3',
            'versioncode': '20141434',
            'market_id': 'floor_baidu',
            '_key': huLuXia["key"],
            'device_code': '%5Bw%5D02%3A00%3A00%3A00%3A00%3A00',
            'cat_id': IDS
        }
        signin_res = session.get(url=url,params=params)
        msg += str(title) +'签到成功\n'
    print(msg)
   
    
def main():
    init()    
    login()  
    check()            
    category()   
    signin()

if __name__ == "__main__":    
    try:
       main()
    except Exception as e:
        push(str(e))

def main_handler(event, context):
    return main()